%% PluVelExample
%
% Rainfall and velocity data management from sample data for tutorial 
% purposes.

% limit dates:
d1 = datenum(2018,3,1);
d2 = datenum(2019,12,31);


%% Rainfall data

disp('Rainfall data management...');
filenaR = 'sampleFiles\Meteo_060120.xlsx';
MR = extraPluvio(filenaR);

M1 = cumPluvio(MR,1,1,d1,d2);
M7 = cumPluvio(MR,7,1,d1,d2);
disp('... done');

% Rainfall data saving. Note the variable fields 

mensaR = menu('DO YOU WANT TO SAVE RAINFALL DATA?','YES','NO');
if mensaR == 1
    [fileR, pathR] = uiputfile( ...
       {'*.mat','MAT-files (*.mat)'}, ...
        'SAVE RAINFALL FILE', 'C1_C7_rainfall.mat');
    fileR = fullfile(pathR,fileR);
    if ischar(fileR)
        if exist(fileR,'file')
            delete(fileR);
        end
        save(fileR,'M1','M7');
    end
end


%% Velocity data

strCell = {'P1','P4','P5','P6','R1'};
CommonPart = 'sampleFiles\Prisma_';     % Windows
% CommonPart = 'sampleFiles/Prisma_';     % Unix
disp('Position data extraction...');
C = extraTarget2cell(strCell,CommonPart);
disp('... done');

% generation of the position array:
disp('Position array generation...');
M = cell2posArray(C,d1,d2);
disp('... done');

% Velocity array with 1-h time step (24 cpd) and mean on 1 d (24 h)
% The fourth argument is unnecessary because the matrices have four
% columns: 
disp('Velocity array generation...');
MVA = velArray(M,1,24,[],strCell);
disp('... done');

% Velocity time series, obtained from targets P4 (2nd in strCell), 
% P5 (3rd) and P6 (4th):
disp('Velocity time series generation...');
MV = selectMeanVelocity(MVA,[2 3 4],d1,d2);
disp('... done');

% Data saving. Please note the variable field:

mensaV = menu('DO YOU WANT TO SAVE VELOCITY DATA?','YES','NO');
if mensaV == 1
    [fileV, pathV] = uiputfile( ...
       {'*.mat','MAT-files (*.mat)'}, ...
        'SAVE VELOCITY FILE','MVelocity.mat');
    fileV = fullfile(pathV,fileV);
    if ischar(fileV)
        if exist(fileV,'file')
            delete(fileV);
        end
        save(fileV,'MV');
    end
end